package com.park.utils;


import com.park.enums.ResultEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

/**
 * http响应结果通用类
 */
@ApiModel(value = "通用响应类")
public class Result<T>{

    // 响应业务状态
    @ApiModelProperty(value = "响应业务状态")
    @Getter
    @Setter
    private Integer code;

    // 响应消息
    @Getter
    @Setter
    @ApiModelProperty(value = "响应消息")
    private String message;

    // 响应中的数据
    @Getter
    @Setter
    @ApiModelProperty(value = "响应中的数据")
    private T data;


    public static <T> Result<T> success(T data) { return restResult(data, ResultEnum.SUCCESS.getCode(), "SUCCESS"); }

    public static <T> Result<T> success() {
        return restResult(null, ResultEnum.SUCCESS.getCode(), "SUCCESS");
    }

    public static <T> Result<T> error(String msg) { return restResult(null, ResultEnum.ERROR.getCode(), msg); }

    public static <T> Result<T> error(ResultEnum error) { return restResult(null, error.getCode(), error.getMessage()); }

    public static <T> Result<T> restResult(T data, int code, String msg) {
        Result<T> apiResult = new Result<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMessage(msg);
        return apiResult;
    }

    public Result() {
        this.code = 200;
        this.message = "SUCCESS";
    }

    public Result(T data) {
        this.code = 200;
        this.message = "SUCCESS";
        this.data = data;
    }

    public Result(ResultEnum resultEnum) {
        this.code = resultEnum.getCode();
        this.message = resultEnum.getMessage();
    }

    public Result(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
}
